package com.david.func_test

import android.content.ContentResolver
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import org.json.JSONException
import org.json.JSONObject

/**
 * 获取华为广告归因
 */
class HwInstallInfoUtil {
    companion object{
        private const val TAG = "huawei"
        private const val PROVIDER_URI = "content://com.huawei.appmarket.commondata/item/5"
        private const val INDEX_ENTER_AG_TIME = 1
        private const val INDEX_INSTALLED_FINISH_TIME = 2
        private const val INDEX_START_DOWNLOAD_TIME = 3
        private const val INDEX_TRACKID = 4

        /**
         * 获取 trackid
         * @param pkgName 目标包名
         * @return json 格式字符串
         */
        fun getTrackId(context: Context, pkgName: String): String? {
            var trackId: String? = null
            var cursor: Cursor? = null
            val uri: Uri = Uri.parse(PROVIDER_URI)
            val contentResolver: ContentResolver = context.getContentResolver()
            val packageName = arrayOf(pkgName)
            try {
                cursor = contentResolver.query(uri, null, null, packageName, null)
                if (cursor != null) {
                    cursor.moveToFirst()
                    Log.i(TAG, "packageName= $pkgName")
                    if (cursor.getColumnCount() > INDEX_TRACKID) {
                        // 10.5.0.300 及之后版本
                        Log.i(
                            TAG, "enter AppGallery time = " +
                                    cursor.getString(INDEX_ENTER_AG_TIME)
                        )
                        Log.i(
                            TAG, "installed time = " +
                                    cursor.getString(INDEX_INSTALLED_FINISH_TIME)
                        )
                        Log.i(
                            TAG, "download time = " +
                                    cursor.getString(INDEX_START_DOWNLOAD_TIME)
                        )
                        Log.i(TAG, "track id = " + cursor.getString(INDEX_TRACKID))
                        trackId = cursor.getString(INDEX_TRACKID)
                    } else {
                        // 不支持归因信息
                        Log.e(TAG, "AppGallery not support")
                    }
                }
            } catch (e: Exception) {
                //处理异常
            } finally {
                if (cursor != null) {
                    cursor.close()
                }
            }
            // 如果 trackid 是 json 格式，打印具体内容
            if (!TextUtils.isEmpty(trackId)) {
                try {
                    val attributionMap = JSONObject(trackId)
                    Log.i(TAG, "json channel id = " + attributionMap.getString("channel"))
                    Log.i(TAG, "json callback = " + attributionMap.get("callback"))
                    Log.i(TAG, "json taskid = " + attributionMap.get("taskid"))
                    Log.i(TAG, "json subTaskId = " + attributionMap.get("subTaskId"))
                    Log.i(TAG, "json RTAID = " + attributionMap.get("RTAID"))
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }
            Toast.makeText(context, "track id is: $trackId", Toast.LENGTH_LONG).show()
            return trackId
        }

        public fun getPhoneBrand():String {

            var manufacturer = Build.MANUFACTURER;
            if (manufacturer != null && manufacturer.isNotEmpty()) {
                return manufacturer.toLowerCase();
            } else {
                return "unknown";
            }

        }

        public fun isHuaWei():Boolean{
            var phoneBrand = getPhoneBrand()
            return phoneBrand.contains("HUAWEI") || phoneBrand.contains("OCE")
                    || phoneBrand.contains("huawei") || phoneBrand.contains("honor");
        }
    }
}